package com.example.ymm.modules1.controller.sys;

import com.example.ymm.model.CommonResult;
import com.example.ymm.model.TableList;
import com.example.ymm.model.YmPageDto;
import com.example.ymm.modules1.controller.BaseController;
import com.example.ymm.modules1.entity.my.sys.SysUser;
import com.example.ymm.modules1.service.dto.SysUserQueryCriteria;
import com.example.ymm.modules1.service.sys.SysUserService;
import io.swagger.v3.oas.annotations.Parameter;
import io.swagger.v3.oas.annotations.media.Schema;
import io.swagger.v3.oas.annotations.tags.Tag;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;

import javax.validation.Valid;

@RestController
@RequestMapping("/hl/sys/account")
@Tag(name= "pc端 系统账号管理")
@Slf4j
public class SysAccountController extends BaseController {

    @Autowired
    SysUserService sysUserService;

    @Schema(name= "列表")
    @RequestMapping(value = "/list", method = RequestMethod.POST)
    public CommonResult list(@Valid SysUserQueryCriteria criteria, YmPageDto ymPage) {
        try{
            TableList<SysUser> list = sysUserService.list(criteria, ymPage);
            return CommonResult.success(list,"获取成功");
        }catch (Exception e){
            log.error(e.getMessage(),e);
            return CommonResult.error(e.getMessage());
        }
    }

    @Schema(name= "保存")
    @RequestMapping(value = "/save", method = RequestMethod.POST)
    public CommonResult save(SysUser sysUser) {
        try{
//            if("manage".equals(sysUser.getUserName())){
//                return CommonResult.error("管理员账号不允许修改!");
//            }
            sysUserService.addUser(sysUser);

            return CommonResult.success();
        }catch (Exception e){
            log.error(e.getMessage(),e);
            return CommonResult.error(e.getMessage());
        }
    }

    @Schema(name= "删除")
    @RequestMapping(value = "/delete", method = RequestMethod.POST)
    public CommonResult delete(@Parameter(name = "数据主键") @RequestParam(required = true) String sysUserId) {
        try{
            SysUser sysUser = sysUserService.getById(sysUserId);
            if("manage".equals(sysUser.getUserName())){
                return CommonResult.error("管理员账号不允许删除!");
            }
            sysUserService.removeById(sysUserId);
            return CommonResult.success();
        }catch (Exception e){
            log.error(e.getMessage(),e);
            return CommonResult.error(e.getMessage());
        }
    }
}
